//
//  findDetailViewController.m
//  A Learning Walk
//
//  Created by 张勇 on 16/6/15.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "findDetailViewController.h"
#import "findDetailTableViewCell.h"
#import "ZYPlayerViewController.h"
#import "DetailListModel.h"
#import "UIColor+AddColor.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface findDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,retain)UIImageView *imageV;
@property(nonatomic,retain)UILabel *labelOne;
@property(nonatomic,retain)UILabel *labelTwo;
@property(nonatomic,retain)UILabel *labelThree;
@property(nonatomic,retain)UILabel *labelFour;
@property(nonatomic,retain)UIButton *buttonOne;
@property(nonatomic,retain)UIButton *buttonTwo;
@property(nonatomic,retain)NSMutableArray *myArray;
@property(nonatomic,retain)NSMutableArray *AVArray;//所有音乐链接
@property(nonatomic,retain)UIImageView *PlayImageV;
@property(nonatomic,assign)BOOL likebool;

@property(nonatomic, retain)MBProgressHUD *HUB;
@end

@implementation findDetailViewController

-(void)dealloc{

    [_myTableView release];
    [_labelOne release];
    [_labelTwo release];
    [_labelThree release];
    [_labelFour release];
    [_buttonOne release];
    [_buttonTwo release];
    [_myArray release];
    [_AVArray release];
    [_PlayImageV release];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"toZYPlayer" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"playChange" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"tabBarOn" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PlayImage" object:nil];
    [super dealloc];

}


-(void)viewWillAppear:(BOOL)animated{
    
    NSMutableArray *ARRAY = [NSMutableArray array];
    NSMutableArray *array = [[DataBaseHandle shareDataFMDBHandle] selectAll];
    for (smallListModel *model in array) {
        //  NSLog(@"=======%@",self.model.title);
       // NSLog(@"+++++%@",model.albumId);
        [ARRAY addObject:model.albumId];
    }
        if ([ARRAY containsObject:self.albumid]) {
            
            [self.buttonOne setImage:[UIImage imageNamed:@"newAblum_pay_rssed-1"] forState:UIControlStateNormal];
           self.buttonOne.selected = NO;
        }else{
            [self.buttonOne setImage:[UIImage imageNamed:@"newAblum_pay_rss"] forState:UIControlStateNormal];
            self.buttonOne.selected = YES;
        }
    [self.myTableView reloadData];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back_h"]  style:UIBarButtonItemStylePlain target:self action:@selector(Action:)];
    
    self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUB.mode = MBProgressHUDModeIndeterminate;//不同类型
    [self.HUB show:YES];
    self.HUB.labelText = @"loading...";
    //获取数据
    [self dataHandle];
    self.myArray = [NSMutableArray array];
    self.AVArray = [NSMutableArray array];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.myTableView.backgroundColor= [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.navigationController.navigationBar.translucent = NO;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTableView release];
    
    
    UINib *nib = [UINib nibWithNibName:@"findDetailTableViewCell" bundle:[NSBundle mainBundle]];
    [self.myTableView registerNib:nib forCellReuseIdentifier:@"cellReuse"];
    //创建透视图
    [self createHeader];

    
    //点击圆形按钮调到播放页面
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playingAction:) name:@"toPlayer" object:nil];
#warning ++++++++++++
        //控制左侧小图片的播放状态
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playAction:) name:@"bool" object:nil];
#warning ++++++++++++
    // Do any additional setup after loading the view.
}

//点击圆形按钮调到播放页面
//-(void)playingAction:(NSNotification *)noti{
//    ZYPlayerViewController *zyPlay = [ZYPlayerViewController shareZYPlayerViewController];
//    UINavigationController *NA = [[UINavigationController alloc] initWithRootViewController:zyPlay];
//     //zyPlay.AllArray = self.myArray;
////     NSArray *array = [NSArray arrayWithArray:self.myArray];
////    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
////    [defaults setObject:array forKey:@"array"];
////   [defaults synchronize];
////    NSLog(@"%@",[defaults objectForKey:@"array"]);
//    [self presentViewController:NA animated:YES completion:^{
//        
//        
//    }];
//    self.tabBarController.tabBar.hidden = YES;
//    
//}
#warning ++++++++++++
//控制左侧小图片的播放状态
//-(void)playAction:(NSNotification *)noti{
//    NSDictionary *dic = noti.object;
//    NSMutableArray *array = [NSMutableArray arrayWithArray:[dic objectForKey:@"array"]];
//    DetailListModel *model = [array objectAtIndex:[[dic objectForKey:@"index"] integerValue]];
//    NSLog(@"isbool === %d",model.isBool);
//    if (model.isBool == 0) {
//        NSLog(@" 000 ==== 走了吗");
//        self.PlayImageV.image = [UIImage imageNamed:@"toolbar_play_n"];
//    }else{
//        self.PlayImageV.image = [UIImage imageNamed:@"toolbar_pause_h"];
//        
//    }

//}


//获取数据
-(void)dataHandle{

    [ZYNetworkHandle GetDataWithURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/album/track?albumId=%@",self.albumid] dic:nil success:^(id responseObject) {
        
        DetailBigModel *bigModel = [DetailBigModel yy_modelWithDictionary:responseObject];
        
        [self.myArray addObjectsFromArray:bigModel.tracks.list];
        
        [self.imageV sd_setImageWithURL:[NSURL URLWithString:bigModel.album.coverOrigin] placeholderImage:[UIImage imageNamed:@"holderr"]];
        self.labelOne.text = bigModel.album.title;
        self.labelTwo.text = [NSString stringWithFormat:@"主播:   %@",bigModel.album.nickname];
        self.labelThree.text = [NSString stringWithFormat:@"播放:   %ld.%.1ld万次",(long)bigModel.album.playTimes / 10000,(long)bigModel.album.playTimes % 10000 / 1000];
       
        //时间戳转换为时间
        NSString *str = [self stringWithDate:[bigModel.album.updatedAt substringWithRange:NSMakeRange(0, 10)]];
        self.labelFour.text = [NSString stringWithFormat:@"状态:   %@更新",str];
        
        self.labelFour.font = [UIFont systemFontOfSize:13];
        self.labelFour.textColor = [UIColor grayColor];
        self.labelTwo.font = [UIFont systemFontOfSize:13];
        self.labelTwo.textColor = [UIColor grayColor];
        self.labelThree.font = [UIFont systemFontOfSize:13];
        self.labelThree.textColor = [UIColor grayColor];
        
       
        self.HUB.hidden = YES;
        [self.myTableView reloadData];
       // NSLog(@"%@",responseObject);
    } filed:^(NSError *error) {
        
        
    }];
    
}
//时间戳转换为时间
-(NSString *)stringWithDate:(NSString *)string{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[string integerValue]];
    
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *str = [formatter stringFromDate:confromTimesp];
    [formatter release];

    return str;

}



//订阅按钮
-(void)buttonAction:(UIButton *)button{
 
       if (self.buttonOne.selected) {
           [self.buttonOne setImage:[UIImage imageNamed:@"newAblum_pay_rssed-1"] forState:UIControlStateNormal];
           [[DataBaseHandle shareDataFMDBHandle]insertTravelsCollectTableWithModel:_model];//插入数据库
        
           NSLog(@"订阅按钮");
           
       }else{
           [self.buttonOne setImage:[UIImage imageNamed:@"newAblum_pay_rss"] forState:UIControlStateNormal];
           [[DataBaseHandle shareDataFMDBHandle] deleteTableWithalbumid:_model.albumId];//删除数据
        
           NSLog(@"阅按钮");
       }
   
    self.buttonOne.selected = !self.buttonOne.selected;
   
}
//创建头视图
-(void)createHeader{

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 2.5)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    UIView *header = [UIView new];
    header.backgroundColor = [UIColor clearColor];
    [headerView addSubview:header];
    
    [header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(headerView);
        
    }];
    self.myTableView.tableHeaderView = headerView;
    
    UIImageView *imageV = [UIImageView new];
    imageV.backgroundColor = [UIColor clearColor];
    [header addSubview:imageV];
    self.imageV = imageV;
    
    UILabel *labelOne = [UILabel new];
    labelOne.backgroundColor = [UIColor clearColor];
    [header addSubview:labelOne];
    self.labelOne = labelOne;
    
    UILabel *labelTwo = [UILabel new];
    labelTwo.backgroundColor = [UIColor clearColor];
    [header addSubview:labelTwo];
    self.labelTwo = labelTwo;
    
    UILabel *labelThree = [UILabel new];
    labelThree.backgroundColor = [UIColor clearColor];
    [header addSubview:labelThree];
    self.labelThree = labelThree;
    
    UILabel *labelFour = [UILabel new];
    labelFour.backgroundColor = [UIColor clearColor];
    [header addSubview:labelFour];
    self.labelFour = labelFour;
    
//    //收藏按钮
    UIButton *buttonOne = [UIButton new];
    buttonOne.backgroundColor = [UIColor colorFromHexCode:@"#7FFF00"];
    buttonOne.layer.masksToBounds = YES;
    buttonOne.layer.cornerRadius = 5;
    [header addSubview:buttonOne];
    //buttonOne.layer.borderColor = [[UIColor colorWithRed:156 / 255 green:204 / 255 blue:101/ 255 alpha:1]CGColor];
  
    
   // [buttonOne setImage:[UIImage imageNamed:@"newAblum_pay_rss"] forState:UIControlStateNormal];
    [buttonOne addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.buttonOne = buttonOne;
   
    
    UIButton *buttonTwo = [UIButton new];
   // buttonTwo.backgroundColor = [UIColor redColor];
    buttonTwo.layer.masksToBounds = YES;
    buttonTwo.layer.cornerRadius = 5;
    buttonTwo.layer.borderWidth = 1;
    buttonTwo.layer.borderColor = [[UIColor colorFromHexCode:@"7FFF00"]CGColor];
    [buttonTwo setImage:[UIImage imageNamed:@"newAblum_free_download"] forState:UIControlStateNormal];
    [header addSubview:buttonTwo];
    self.buttonTwo = buttonTwo;
    
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(headerView.frame.size.width / 2.5, (headerView.frame.size.height - 20) / 3 * 2));
    }];
    
    [labelOne mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(imageV.mas_right).offset(10);
        make.top.mas_equalTo(imageV);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40);
    }];
    
    [labelTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(labelOne.mas_bottom).offset(0);
        make.left.right.height.mas_equalTo(labelOne);
    }];
    
    [labelThree mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(labelTwo.mas_bottom).offset(0);
        make.left.right.height.mas_equalTo(labelTwo);
    }];
    
    [labelFour mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(labelThree.mas_bottom).offset(0);
        make.left.right.height.mas_equalTo(labelThree);
        
    }];
    
    [buttonOne mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(imageV.mas_bottom).offset(25);
        make.left.mas_equalTo(imageV);
        make.height.mas_equalTo(40);
        make.right.mas_equalTo(buttonTwo.mas_left).offset(-30);
        make.width.mas_equalTo(buttonTwo);
    }];
    
    [buttonTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(buttonOne);
        make.left.mas_equalTo(buttonOne.mas_right).offset(30);
        make.height.with.mas_equalTo(buttonOne);
        make.right.mas_equalTo(-20);
        
    }];
    
}


//跳转详情页面
-(void)Action:(UIBarButtonItem *)bar{
[self.navigationController popViewControllerAnimated:YES];

}
//跳转到播放页面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ZYPlayerViewController *zyVC = [ZYPlayerViewController shareZYPlayerViewController];
    DetailListModel *model = [self.myArray objectAtIndex:indexPath.row];
    //点击该当cell改变bool值
    //model.isBool = YES;
    //[[NSNotificationCenter defaultCenter] postNotificationName:@"bool" object:@"1"];
    //获取所有音乐链接
    for (DetailListModel *avModel in self.myArray) {
        [self.AVArray addObject:avModel.playUrl64];
    }
    //往HRPlayView发送通知并携带一个字典
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:model.coverLarge forKey:@"picture"];
    [dic setObject:@"1" forKey:@"play"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"playOrStop" object:dic];
    
   // NSLog(@"=====++++++++++======%hhd",model.isBool);
    zyVC.indexRow = indexPath.row;
    zyVC.AVArray = self.AVArray;
    zyVC.AllArray = self.myArray;
    zyVC.model = model;
    UINavigationController *zyNA = [[UINavigationController alloc] initWithRootViewController:zyVC];
    [self presentViewController:zyNA animated:YES completion:^{
        
        
    }];
    [zyVC release];
    [zyNA release];
}
//分区头标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return [NSString stringWithFormat:@"节目列表"];

}
//section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}
//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;

}
//cell行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.myArray.count;
}
//显示cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    findDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellReuse" forIndexPath:indexPath];
    
    DetailListModel *model = [self.myArray objectAtIndex:indexPath.row];
    //model.isBool = YES;
    cell.model = model;
   // NSLog(@"cellbool === ==== %d",cell.model.isBool);
    //NSLog(@"%d",model.isBool);
#warning ++++++++++++
//    if (model.isBool == 0) {
//        NSLog(@" 000 ==== 吗");
//        [self.buttonOne setImage:[UIImage imageNamed:@"newAblum_pay_rss"] forState:UIControlStateNormal];
//    }else{
//        [self.buttonOne setImage:[UIImage imageNamed:@"newAblum_pay_rssed-1"] forState:UIControlStateNormal];
//        
//    }
//    model.isBool = !model.isBool;
#warning ++++++++++++
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        return cell;


}
//取消分区头吸附效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.myTableView)
    {
        CGFloat sectionHeaderHeight = 60; //sectionHeaderHeight
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
